<?php

namespace app\admin\model\xiluedu;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class Course extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xiluedu_course';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'is_charge_text',
        'is_hot_text',
        'is_new_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::beforeWrite(function ($row){
            if($row->category_id){
                $category = Category::where('id',$row->category_id)->find();
                $row->category_ids = $category ? $category->path : '';
            }
            if($row->is_charge > 0 && $row->salesprice < 0.01){
                throw new Exception("付费课程购买价格不得小于0.01");
            }
        });
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')];
    }

    public function getIsChargeList()
    {
        return ['0' => __('Is_charge 0'), '1' => __('Is_charge 1'), '2' => __('Is_charge 2')];
    }

    public function getIsHotList()
    {
        return ['0' => __('Is_hot 0'), '1' => __('Is_hot 1')];
    }

    public function getIsNewList()
    {
        return ['0' => __('Is_new 0'), '1' => __('Is_new 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsChargeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_charge']) ? $data['is_charge'] : '');
        $list = $this->getIsChargeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsHotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_hot']) ? $data['is_hot'] : '');
        $list = $this->getIsHotList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsNewTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_new']) ? $data['is_new'] : '');
        $list = $this->getIsNewList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function category(){
        return $this->belongsTo(Category::class,'category_id','id',[],'left')->setEagerlyType(0);
    }

}
